
package com.trydofor.id.body.shell;

import com.trydofor.id.byta.Bytable;
import com.trydofor.id.byta.ByteMeta;
import com.trydofor.id.byta.Bytes;

/**
 * command usage
 * 
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public class UsageQueryBody implements Bytable<UsageQueryBody> {
    
    private final String command;
    
    /**
     * constructor
     * 
     * @param command the command to query, null means all
     */
    public UsageQueryBody(String command) {
        this.command = command;
    }
    
    /**
     * @return the command
     */
    public String getCommand() {
        return command;
    }
    
    /**
     * {@inheritDoc}
     */
    public byte[] toBytes() throws Exception {
        return Bytes.toMetaBytes(command);
    }
    
    /**
     * {@inheritDoc}
     */
    public UsageQueryBody ofBytes(byte[] bytes, int offset, int length) throws Exception {
        ByteMeta<UsageQueryBody> bm = ofBytes(bytes, offset);
        return (bm.byteSize() != length) ? null : bm.instance();
    }
    
    /**
     * {@inheritDoc}
     */
    public ByteMeta<UsageQueryBody> ofBytes(byte[] bytes, int offset) throws Exception {
        ByteMeta<String> bm = Bytes.ofMetaBytes(bytes, offset);
        return ByteMeta.of(new UsageQueryBody(bm.instance()), bm.byteSize());
    }
}
